package sfmc.common.util;

//base class
import java.text.DecimalFormat;
import java.text.ParseException;

public class NumberUtil {

	public static boolean isNumeric(String str) {
		try{ 
			Integer.parseInt(str);
		} catch (NumberFormatException ex) {
			return false;
		}
		return true;
	}

	public static boolean isCurrency(String str) {
		try{
			new DecimalFormat().parse(str.trim());
		} catch (ParseException ex) {
			return false;
		}
		return true;
	}

	public static int curToInt(String arg) {
		try {
			return new DecimalFormat().parse(arg.trim()).intValue();
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return -1;
	}

	public static long curToLong(String arg) {
		try {
			return new DecimalFormat().parse(arg.trim()).longValue();
		} catch (ParseException ex) {
			ex.printStackTrace();
		}
		return -1;
	}

	  public static int getIntValue(Object src, int defValue) {
	    String v_value;
	    if (src != null) {
	      try {
	        v_value = (String)src;
	        return Integer.parseInt(v_value);
	      } catch (Exception e) {
	        return defValue;
	      }
	    } else {
	      return defValue;
	    }
	  }

	  public static int getIntValue(Object src) {
	    if (src != null) {
	      try {
	        return getIntValue(StringUtil.trim(src,"0"),0);
	      } catch (Exception e) {
	        return 0;
	      }
	    } else {
	      return 0;
	    }
	  }

	  public static long getLongValue(Object src, long defValue) {
	    String v_value;
	    if (src != null) {
	      try {
	        v_value = (String)src;
	        return Long.parseLong(v_value);
	      } catch (Exception e) {
	        return defValue;
	      }

	    } else {
	      return defValue;
	    }

	  }

	  public static long getLongValue(Object src) {
	    if (src != null) {
	        try {
	          return getLongValue(StringUtil.trim(src,"0"),0);
	        } catch (Exception e) {
	          return 0;
	        }
	      } else {
	        return 0;
	      }
	  }

	  public static float getFloatValue(Object src, float defValue) {
	    String v_value;
	    if (src != null) {
	      try {
	        v_value = (String)src;
	        return Float.parseFloat(v_value);
	      } catch (Exception e) {
	        return defValue;
	      }

	    } else {
	      return defValue;
	    }
	  }

	  public static boolean getBoolValue(String key, boolean defValue) {
	    if (key == null) {
	      return defValue;
	    } else {
	      if (key.equalsIgnoreCase("true")) { return true; }
	      else { return false; }
	    }
	  }
  

}
